package com.lpwa.web.controller;

import com.alibaba.fastjson.JSON;
import com.lpwa.common.config.Global;
import com.lpwa.system.domain.Person;
import com.lpwa.system.domain.TagAlarmData;
import com.lpwa.system.domain.tagdata.position.TagOne;
import com.lpwa.system.domain.tagdata.position.TagPositiont;
import com.lpwa.system.domain.tagdata.position.Zone;
import com.lpwa.system.service.IPersonService;
import com.lpwa.system.service.ITagAlarmDataService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Controller
public class Indexskip {
    @Autowired
    private ITagAlarmDataService tagAlarmDataService;
    @Autowired
    private IPersonService personService;
    @RequestMapping("/test")
    public String index(){
        return "test";
    }

    @RequestMapping("/test1")
    public String index1(){
        return "test1";
    }
    @RequestMapping("/mapIndex")
    public String mapIndex(){
        return "mapIndex";
    }
    @RequestMapping("/mapIndex/{id}")
    public String mapIndex(@PathVariable("id") String id, ModelMap mmap){
        mmap.put("tagId", id);
        return "mapIndex";
    }
    @RequestMapping("/mapLocation/{id}")
    public String mapLocation(@PathVariable("id") Long id, ModelMap mmap){
        TagAlarmData tagAlarmData = tagAlarmDataService.selectTagAlarmDataById(id);
        BigDecimal x = tagAlarmData.getX();
        BigDecimal y = tagAlarmData.getY();
        BigDecimal z = tagAlarmData.getZ();
        List<TagOne> listTagOne = new ArrayList<TagOne>();
        List<BigDecimal> position = new ArrayList<BigDecimal>();
        position.add(x);
        position.add(y);
        position.add(z);
        TagPositiont tagPositiont = new TagPositiont();
        TagOne tagOne = new TagOne();
//        tagOne.setPosition(position);
        String httpAddress = Global.getTdmpLeIpAndPort();
        String coordinateSystemId ="";
//        try {
//            String project = HttpUtils.sendPost(" http://"+httpAddress+"/qpe/getProjectInfo","version=2&noImageBytes=true&humanReadable=true");
//            ProjectInfo projectInfo = JSON.parseObject(project, ProjectInfo.class);
//            coordinateSystemId = projectInfo.getCoordinateSystems().get(0).getId();
//        }catch (Exception e){
//            System.out.println(e.getMessage());
//        }
        coordinateSystemId="f34fa8ba-e3ce-41d9-a6d5-e0a8e60bbb3d";
        tagOne.setCoordinateSystemId(coordinateSystemId);
        tagOne.setId(tagAlarmData.getTagId());
        tagOne.setPositionTS(new Date().getTime());
        tagOne.setSmoothedPosition(position);
        tagOne.setZones(new ArrayList<Zone>());
        listTagOne.add(tagOne);
        tagPositiont.setTags(listTagOne);
        tagPositiont.setResponseTS(new Date().getTime());
        tagPositiont.setVersion("2.1");
        mmap.put("tagPositiont", JSON.toJSON(tagPositiont));
        return "system/map/mapLocation";
    }
//    @RequestMapping("/mapLocation")
//    public String mapLocation(){
//        return "system/map/mapLocation";
//    }
    @RequestMapping("/electronicFence")
    public String electronicFence(ModelMap mmap){
        //查询人员信息
        List<Person> list = personService.selectPersonList(new Person());
        mmap.put("personList", JSON.toJSON(list));
        return "system/map/electronicFence";
    }

    @RequestMapping("/fence")
    public String fence(){

        return "system/map/fence";
    }
    @RequestMapping("/addFence")
    public String addFence(){

        return "system/map/addFence";
    }

    @RequestMapping("/dataDept")
    public String dataDept(){

        return "system/map/dataDept";
    }
    @RequestMapping("/mapTrack")
    public String mapTrack(ModelMap mmap){
        return "system/map/mapTrack";
    }
}